// pages/myData/myData.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickName:'',
    sex:'',
    phone:'',
    address:'',
    introduce:'无',
    realName:'',
    headImgURL:''
  },
  getPic(e){
    wx.chooseImage({
      count: 1,
      success:res=>{
        wx.showLoading({
          title: '图片上传中 ',
        })
        var pic = ''
  
        pic = res.tempFilePaths[0]
       
   
        wx.uploadFile({
          filePath: pic,
          name:'file',
          header: {
            "Content-Type": "multipart/form-data",
            Authorization:app.globalData.header
           },
      
          url:app.globalData.Baseip+":8881/file/upload?fileName="+pic,
          success:res=>{
            var url = JSON.parse(res.data).data
            // console.log(url)
            console.log(res)
            // console.log(index,piclen)

           
              this.setData({
                headImgURL:url
              })
              this.postImg(url)
              wx.hideLoading()
            
          },
          fail:err=>{
            console.log(err)
          }
        })
      
       
       
      },
      fail:err=>{

      }
    })

  },
  postImg(url){
    wx.request({
      url: app.globalData.Baseip+':8880/user/updateUser',
      data:{
        "headImgUrl": url
      },
      method:'POST',
      header: {
       Authorization:app.globalData.header
      },
      success:function(res){
        console.log(res)
       
      },
      fail:function(err){
        console.log(err)
      }
    })
  },
  changeAvatar(){
    //选取照片
    this.getPic()
    //替换
   
    //发送
  },
  toNickName(e){
    var nickName = e.currentTarget.dataset.nickname
    wx.navigateTo({
      url: '../test/nickName?nickName='+nickName,
    })
  },
  toGender(){
    wx.navigateTo({
      url: '../test/Gender',
    })
  },
  toTelephone(e){
    var phone = e.currentTarget.dataset.phone
    wx.navigateTo({
      url: '../test/phone?phone='+phone,
    })
  },
  toAddress(e){
    var address = e.currentTarget.dataset.address
    wx.navigateTo({
      url: '../test/address?address='+address,
    })
  },
  toIntroduce(){
    wx.navigateTo({
      url: '../test/introduce',
    })
  },
  getMyData(){
    wx.request({
      url:  app.globalData.Baseip+':8880/user/getUserInformation',
      header: {
        Authorization:app.globalData.header
      },
      method:'GET',
      success:(res)=>{
        var data = res.data.data
        console.log(res)
        this.setData({
          nickName:data.nickName,
          introduce:data.introduce,
          address:data.address,
          phone:data.phone,
          sex:data.sex,
          headImgURL:data.headImgUrl,

        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMyData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})